package com.zx.jhpt.service;

import com.zx.framework.core.pagination.Pagination;
import com.zx.framework.core.util.BeanUtil;
import com.zx.framework.core.util.DatetimeUtils;
import com.zx.framework.core.util.SQLBuilder;
import com.zx.framework.core.util.StringUtil;
import com.zx.framework.db.IBaseDAO;
import com.zx.framework.db.exception.DatabaseException;
import com.zx.framework.plat.user.vo.UserVo;
import com.zx.jhpt.po.Replymessage;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component("replyMessageService")
@Transactional
public class ReplyMessageService {

    private static final Logger LOGGER = Logger.getLogger(ReplyMessageService.class);
    @Resource
    private IBaseDAO baseDAO;
    @Resource
    private FxDeptService fxDeptService;

    public void save(Replymessage replymessage) {
        baseDAO.save(replymessage);
    }

    
    public List<Map> getReplyMeByHcjs(String fxid, String hcsj,String deptId) {
        String sql = "select * from enclosure e where e.fxid = ? and to_char(e.hcsj,'yyyy-MM') = ? and deptid = ?";
    	return baseDAO.querySQL(sql,fxid, hcsj,deptId);
    }

    public Pagination<Replymessage> page(Replymessage reply, UserVo queryUser, Integer page, Integer rows) {
        try {
            String qryHql = "from Replymessage t,Fxfkjz f where 1=1 and t.fxid=f.id ";
            String cntHql = "select count(*) from Replymessage t where 1=1 ";

            List<Object> params = new ArrayList<>();
            List<String> clauses = new ArrayList<>();
            //条件查询可以放在这里
            if (reply != null) {
                if (null != reply.getHcsj()) {
                    clauses.add(" and t.hcsj=?");
                    params.add(reply.getHcsj());
                }
                if (StringUtil.isNotBlank(reply.getDeptid())) {
                    clauses.add(" and t.deptid=?");
                    params.add(reply.getDeptid());
                }
                if(null!=reply.getStatus()){
                    clauses.add(" and t.status=?");
                    params.add(reply.getStatus());
                }
            }
            
            if(EngineService.assignDept.equals(queryUser.getDept().getId())){
            	clauses.add(" and (exists (from FxUser fu where t.fxid=fu.id.fxid and t.deptid=fu.id.dept and fu.id.userid=? )  ");
            	clauses.add("or exists( from ReplyUser ru where t.id = ru.id.replyid and cycle=? and ru.id.userid=?))");
            	params.add(queryUser.getId());
            	params.add(DatetimeUtils.dateToString(reply.getHcsj(), "yyyy-MM"));
            	params.add(queryUser.getId());
            }else{
            	clauses.add(" and exists (from FxUser fu where t.fxid=fu.id.fxid and t.deptid=fu.id.dept and fu.id.userid=? ) ");
            	params.add(queryUser.getId());
            }
            
            

            if (!clauses.isEmpty()) {
                String where = SQLBuilder.appendSQL(clauses);
                qryHql += where;
                cntHql += where;
            }
            return baseDAO.hqlPageQuery(qryHql, cntHql, page, rows, params.toArray());
        } catch (Exception e) {
            LOGGER.error("查询反馈列表失败！", e);
            throw new DatabaseException("查询反馈列表失败！");
        }
    }

    /**
     * 查看反馈表中是否有数据
     */
    public Replymessage findReplyMessage(String pkid,String hcsj,String deptid){
        String hql = "from Replymessage r where r.fxid = ? and to_char(r.hcsj,'yyyy-MM')=? and r.deptid=?";
        return baseDAO.getHQLUnique(hql,Replymessage.class,pkid,hcsj,deptid);
    }
	/**
     * 风险详情
     * @param replyId
     * @return
     */
    public Object find(String replyId,String deptId) {
        String hql = "from Replymessage t,Fxfkjz f where 1=1 and t.fxid=f.id " +
                "  and t.id=? and t.deptid = ? ";
        return baseDAO.getHQLUnique(hql,Object.class,replyId,deptId);
    }

    /**
     * 通过ID查找
     * @param id id
     * @return Replymessage
     */
    public Replymessage findById(String id){
        return baseDAO.findById(id,Replymessage.class);
    }

    public void update(Replymessage replymessage) {
        Replymessage r = findById(replymessage.getId());
        BeanUtil.extend(replymessage,r,"id","hcsj","fxid","deptid","deptname");
        baseDAO.update(r);
    }
}
